package 链表;
class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public class 两两交换 {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        int i = 0;
        ListNode tmp = head;
        while(i<=4){
            tmp.next = new ListNode(i);
            tmp = tmp.next;
            i++;
        }

        swapPairs(head);
    }


    public static ListNode swapPairs(ListNode head) {
        if(head == null) return head;
        ListNode newhead = head;
        if(newhead.next!=null) newhead = newhead.next;

        ListNode pre = new ListNode(0);
        ListNode cur = head;
        ListNode tmp = null;

        while(cur!=null&&cur.next!=null){
            tmp = cur.next;
            // 画图
            cur.next = tmp.next;
            tmp.next = cur;
            pre.next = tmp;
            pre = cur;

            cur = cur.next;
        }
        return newhead;
    }
}
